#include <stdint.h>
#include <stdio.h>

#include <pthread.h>
#include <unistd.h>


pthread_barrier_t g_stThreadBarrier;

void* __thread_func(void* pArg)
{
    uint32_t uiIndex = (uint32_t)(unsigned long)pArg;

    printf("Thread %u will sleep running\n", uiIndex);
    sleep(uiIndex + 1);
    printf("Thread %u is running\n", uiIndex);
    pthread_barrier_wait(&g_stThreadBarrier);

    printf("Thread %u do something\n", uiIndex);

    return 0;
}

int main(int argc, char** argv)
{
    pthread_t astThread[8];

    pthread_barrier_init(&g_stThreadBarrier, NULL, 8);

    for (uint32_t uiIndex = 0; uiIndex < 8; uiIndex++)
    {
        pthread_create(&astThread[uiIndex], NULL, __thread_func, (void *)(unsigned long)uiIndex);
    }

    for (uint32_t uiIndex = 0; uiIndex < 8; uiIndex++)
    {
        pthread_join(astThread[uiIndex], NULL);
    }

    pthread_barrier_destroy(&g_stThreadBarrier);
    return 0;
}